package com.huangguolin.cloud.user.service.impl;

import com.huangguolin.cloud.mybatis.entity.PtUserEntity;
import com.huangguolin.cloud.mybatis.flexigrid.PadingRstEntity;
import com.huangguolin.cloud.mybatis.flexigrid.PagingEntity;
import com.huangguolin.cloud.mybatis.ztree.ZtreeEntity;
import com.huangguolin.cloud.user.entity.SearchUserEntity;
import com.huangguolin.cloud.user.repository.UserDao;
import com.huangguolin.cloud.user.service.UserService;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Locale;



@Service
public class UserServiceImpl implements UserService {

    @Resource
    private PasswordEncoder passwordEncoder;

    @Resource
    private UserDao userDao;
    @Override
    public PadingRstEntity<PtUserEntity> queryUserInfoBypage(SearchUserEntity entity, PagingEntity paging) {
        PadingRstEntity<PtUserEntity> ptUserEntityPadingRstEntity = new PadingRstEntity<>();
        ptUserEntityPadingRstEntity.setPage(paging.getPage());
        paging.dealColums(PtUserEntity.class);
        entity.deal();
        List<PtUserEntity> list = userDao.queryUserInfoBypage(entity,paging);
        Integer total = userDao.queryUserInfoTotal(entity);
        ptUserEntityPadingRstEntity.setRawRecords(list);
        ptUserEntityPadingRstEntity.setTotal(total);
        ptUserEntityPadingRstEntity.putItem();
        return ptUserEntityPadingRstEntity;
    }
    @Resource
    private MessageSource messageSource;
    @Override
    public List<ZtreeEntity> queryRoleZtreeAll() {
        List<ZtreeEntity> list = userDao.queryRoleZtreeAll();
        ZtreeEntity ztreeEntity = new ZtreeEntity();
        ztreeEntity.setId(ZtreeEntity.ROLE_ZTREE_BOOT_NODE);
        Locale locale = LocaleContextHolder.getLocale();
        String msgStr = messageSource.getMessage("user.role.manager", null, locale);
        ztreeEntity.setName(msgStr);
        list.add(ztreeEntity);
        return list;
    }

    @Override
    public void saveRoleRefUser(String userIds, String roleIds) {
       String[] userArray = userIds.split(",");
       String[] roleArray = roleIds.split(",");
       for (String userId:userArray){
           userDao.deleteRoleById(userId);
           for (String roleId:roleArray){
               userDao.inserRoleByUser(userId,roleId);
           }
       }
    }

    @Override
    public List<ZtreeEntity> queryRoleZtreeByUser(String userId) {

        List<ZtreeEntity> list = queryRoleZtreeAll();
        List<String> checkRoles = userDao.queryRoleZtreeByUser(userId);
        for (ZtreeEntity ztreeEntity:list){
            for (String roleId:checkRoles){
                if(roleId.equals(ztreeEntity.getId())){
                    ztreeEntity.setChecked(true);
                }
            }
        }
        return list;
    }

    @Override
    public void resetPasswordByUser(String userId,String resetPass) {
        String resetPass1 = passwordEncoder.encode(resetPass);
        userDao.resetPasswordByUser(userId,resetPass1);
    }

    @Override
    public void resetPasswordAll(String userId,String resetPass) {
        String resetPass1 = passwordEncoder.encode(resetPass);
        userDao.resetPasswordByUser(userId,resetPass1);
    }

}
